<?php

// +----------------------------------------------------------------------
// | Description: redis 缓存封装使用
// 使用的时候只需要在具体的控制器里面
//   use redis\RedisPackage;
//   $redis= new RedisPackage();
//   $a = $redis->set("123","121231233");
//   echo  $redis->get("123");die;
// +----------------------------------------------------------------------
// | Author: phpAndy <748507607@qq.com>
// +----------------------------------------------------------------------

namespace Baidu;

//百度api获取身份证图片正反面信息
class Baidu
{
    //获取百度api token
    /**
     * @return mixed
     */
    public function token(){
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type']       = 'client_credentials';
        $post_data['client_id']      = 'uqcwHPs6K9sMhKFeVMoYvCD3';
        $post_data['client_secret'] = 'Y3dskwcxTe4AO8VbL5LP8qVA5MkW7rGZ';
        $res = $this->send_post($url,$post_data);
        $data=json_decode($res);
        //   var_dump($data);
        return $data->access_token;
    }
    //post请求
    function send_post($url, $post_data) {
        $postdata = http_build_query($post_data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;

    }

    /**
     * @param string $url
     * @param string $param
     * @return bool|mixed
     */
    function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($curl);//运行curl
        curl_close($curl);
        return $data;
    }


    /**
     * @param string $url
     * @param string $param
     * @return bool|mixed
     */
    function request_posts($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $postUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);

        return $data;
    }

    /**
     *
     * $img拍摄图片
     * $card_side正反面选择
     */
    public function card($img,$card_side){
        $token = $this->token();
        $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' . $token;
        $img = file_get_contents($img);
        $img = base64_encode($img);
        $bodys = array(
            "image" => $img,
            "id_card_side" => $card_side,
            "detect_direction" => true,
            "detect_risk" => true,
            // "id_card_side" => 'back',//反面   'front'//正面''
        );
        $res = $this->request_posts($url, $bodys);
        return $res;
        //var_dump($res);exit;
    }


}
